package com.whmnx.main.ui.model;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.databinding.ObservableField;

import com.whmnx.base.bean.LoginBean;
import com.whmnx.base.bean.ProductBean;
import com.whmnx.base.constants.Constants;
import com.whmnx.base.http.CallBack;
import com.whmnx.base.http.HttpClient;
import com.whmnx.main.R;
import com.whmnx.main.ui.activity.CommodityDetailsActivity;
import com.whmnx.main.ui.fragment.AddCommodityFragment;

import java.util.HashMap;
import java.util.Map;

import me.goldze.mvvmhabit.base.ItemViewModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;

/**
 * author : huang
 * date   : 2020/9/27 0027 15:33
 * version: 1.0
 * @author Administrator
 */
public class ManagementItemViewModel extends ItemViewModel {

    public ObservableField<ProductBean> entity = new ObservableField<>();

    public ObservableField<String> Product_SPEC = new ObservableField<>("");
    public ObservableField<String> Product_Price = new ObservableField<>("");
    public ObservableField<String> Product_Name = new ObservableField<>("");
    public ObservableField<String> Product_Shang = new ObservableField<>("");

    public String imgUrl;
    public int image;
    public Drawable drawableImg;
    public  ProductBean productBean;
    public  ManagementModel viewModel;

    public ManagementItemViewModel(@NonNull ManagementModel viewModel, ProductBean productBean) {
        super(viewModel);
        this.viewModel = viewModel;
        this.entity.set(productBean);
        this.productBean = productBean;
        Product_Name.set(productBean.Product_Name);
        if (productBean.SPECModel.size() > 0) {
            Product_SPEC.set("规格：" + productBean.SPECModel.get(0).ProductSPEC_Name);
            Product_Price.set("￥" + productBean.SPECModel.get(0).ProductSPEC_Price);
        }
        if (!TextUtils.isEmpty(productBean.Product_Image)){
            String[] split = productBean.Product_Image.split(",");
            String url = split[0];
            this.imgUrl = url.contains(Constants.HOST) ? url : Constants.HOST + url;
        }

        if (productBean.Product_Status == 0){
            drawableImg = ContextCompat.getDrawable(viewModel.getApplication(), R.mipmap.shangjia2);
            Product_Shang.set("下架");
        }else {
            drawableImg = ContextCompat.getDrawable(viewModel.getApplication(), R.mipmap.shangjia1);
            Product_Shang.set("上架");
        }
    }



    //订单详情
    public BindingCommand OnClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            Bundle bundle  =   new Bundle();
            bundle.putString("Product_Id",entity.get().Product_Id);
            viewModel.startActivity(CommodityDetailsActivity.class, bundle);
          //  viewModel.startContainerActivity(CommodityDetailsFragment.class.getCanonicalName());
        }
    });

    //编辑
    public BindingCommand OnClickCommand2 = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            Bundle bundle =  new Bundle();
            bundle.putSerializable("entity",productBean);
            viewModel.startActivity(AddCommodityFragment.class,bundle);
        }
    });
    //上架
    public BindingCommand OnClickCommand3 = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            Map<String, Object> params = new HashMap<>();
            params.put("ProductId",entity.get().Product_Id);
            if (Product_Shang.get().equals("上架")){
                params.put("Status","0");
            }else {
                params.put("Status","1");
            }
            HttpClient.get(viewModel.mActivity, Constants.UPDPRODUCTSTATUS, params,new CallBack<LoginBean>() {
                @Override
                public void onSuccess(LoginBean result) {
                    viewModel.getProductmanager(true,"",2,"asc",2);
                }
            });
        }
    });
}
